package platzerworld.kegeln.gui.kegelauswahl;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import platzerworld.kegeln.R;
import platzerworld.kegeln.common.ConstantsIF;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

/**
 * KegelverwaltungSensor
 * 
 * @author platzerg
 */
public class AuswahlKegel extends Activity implements ConstantsIF{
	private int mSelectedWurf;
	private List<ToggleButton> kegeln = new ArrayList<ToggleButton>();
	private String[] clickedKegel = new String[9];
	private int mAnlageTyp = 0;
	private String mLetztesKegelbild;
	
	private Button mSauButton;
	private Button mStierButton;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kegelauswahl);
        
        Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		
		mSelectedWurf =  extras.getInt(INTENT_EXTRA_WURF);
		mAnlageTyp =  extras.getInt(INTENT_EXTRA_ANLAGE_TYP);
		mLetztesKegelbild =  extras.getString(INTENT_EXTRA_KEGELBILD);
			
		
		init();
        
    }
    
    @Override
	public void finish() {
		Intent data = new Intent();
		data.putExtra(INTENT_EXTRA_WURF, mSelectedWurf);
		data.putExtra(INTENT_EXTRA_WURF_WERT, getClickedKegel());
		data.putExtra(INTENT_EXTRA_WURF_KEGEL, getClickedKegelWurf());
		
		setResult(RESULT_OK, data);
		super.finish();
	}
    
    @Override
	protected void onStart() {
		super.onStart();
	}
	
	@Override
	protected void onDestroy() {
		cleanDatabase();
		super.onDestroy();
	}
	
	@Override
	protected void onResume() {
		bindWidgets();
		super.onResume();
	}
	
	
	
	private void init(){
		initWidgets();
		initStyle();
		initListener();
		initContextMenu();
		initDatabase();
	}
	
	private void bindWidgets(){
		
		
	}
	
	private void initStyle() {
		
	}
	
	private void initWidgets(){
		kegeln.add( 0, (ToggleButton) findViewById(R.id.kegel1));
		kegeln.add( 1, (ToggleButton) findViewById(R.id.kegel2));
		kegeln.add( 2, (ToggleButton) findViewById(R.id.kegel3));
		kegeln.add( 3, (ToggleButton) findViewById(R.id.kegel4));
		kegeln.add( 4, (ToggleButton) findViewById(R.id.kegel5));
		kegeln.add( 5, (ToggleButton) findViewById(R.id.kegel6));
		kegeln.add( 6, (ToggleButton) findViewById(R.id.kegel7));
		kegeln.add( 7, (ToggleButton) findViewById(R.id.kegel8));
		kegeln.add( 8, (ToggleButton) findViewById(R.id.kegel9));
		
		mSauButton = (Button) findViewById(R.id.sf_sau);
		mStierButton = (Button) findViewById(R.id.sf_stier);
		
		initAbrKegel();
	}
	
	private void initListener(){	
		for (ToggleButton toggleButton : kegeln) {
			toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					int idx = Integer.valueOf(buttonView.getText().toString());
					if(isChecked){						
						clickedKegel[idx-1] = buttonView.getText().toString();
					}else{
						clickedKegel[idx-1] = "";
					}
				}
			});
		}
		
		mSauButton.setOnClickListener(mSauClickListener);
		mStierButton.setOnClickListener(mStierClickListener);
	}
	
	private void initContextMenu(){
	}
	
	private void initDatabase(){
	}
	
	private void cleanDatabase(){
	}
	
	private int getClickedKegel(){
		int erg = 0;
		for (int i = 0; i < clickedKegel.length; i++) {
			String n = clickedKegel[i];
			if(null != n && !"".equals(n)){
				erg++;
			}
		}
		return erg;
	}
	
	private String getClickedKegelWurf(){
		StringBuffer bf = new StringBuffer();
		for (int i = 0; i < clickedKegel.length; i++) {
			String n = clickedKegel[i];
			if(null != n){
				bf.append(n).append(" ");
			}
		}
		return bf.toString().trim();
	}
	
	private final OnClickListener mSauClickListener = new OnClickListener() {
		public void onClick(View v) {
			sauClicked();
		}
	};
	
	private void sauClicked(){
		for (ToggleButton toggleButton : kegeln) {
			toggleButton.setChecked(true);
		}
	}
	
	private final OnClickListener mStierClickListener = new OnClickListener() {
		public void onClick(View v) {
			stierClicked();
		}
	};
	
	private void stierClicked(){
		kegeln.get(0).setChecked(true);
		kegeln.get(4).setChecked(true);
		kegeln.get(8).setChecked(true);
	}
	
	private void initAbrKegel(){
		StringTokenizer k = new StringTokenizer(mLetztesKegelbild);
		if(k.countTokens() == 9){
			while(k.hasMoreTokens()){
				String nextToken = k.nextToken();
				int l = Integer.parseInt(nextToken);
				kegeln.get(l-1).setChecked(false);
			}
		}else{
			while(k.hasMoreTokens()){
				String nextToken = k.nextToken();
				int l = Integer.parseInt(nextToken);
				ToggleButton letzter = kegeln.get(l-1);
				clickedKegel[l-1] = nextToken;
				letzter.setChecked(!letzter.isChecked());
				letzter.setClickable(false);
			}
		}
		
	}
}
